﻿using ImperialServer.Common.Entities;
using ImperialUtilities.Exceptions;
using ImperialUtilities.Utilities;
using ImperialWebSite.Content.Resources;
using ImperialWebSite.Controllers;
using ImperialWebSite.Presenters.Status.Interfaces;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace ImperialWebSite.Presenters.Status.Classes
{
    public class QueryStatusesPresenter
    {
        private IQueryStatuses _view;
        private StatusController _controller;
        private EventViewer _logger;

        /// <summary>
        /// Contructor de la clase
        /// </summary>
        /// <param name="view">Interfaz que enlaza con la vista</param>
        public QueryStatusesPresenter(IQueryStatuses view)
        {
            _view = view;
            _controller = new StatusController();
            _logger = new EventViewer("ImperialWebSite.Presenters.Status.Classes");
        }

        /// <summary>
        /// Metodo que hace la carga de los estatus existentes en la base de datos
        /// </summary>
        internal void LoadCurrentStatuses()
        {
            try
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.enterMessageInfo, "LoadCurrentStatuses"));

                DataTable dtStatuses = _controller.SelectAllStatuses();
                _view.SessionCurrentStatuses = dtStatuses;
                _view.StatusesDataSource = dtStatuses;

                HttpContext.Current.Trace.Write(Messages.successfulTransactionMessage);
            }
            catch (ImperialException ie)
            {
                _view.SuccessfulTransaction = false;
                _view.TransactionMessage = ie.ImperialMessage;
            }
            catch (Exception e)
            {
                _view.SuccessfulTransaction = false;
                _view.TransactionMessage = Messages.failureTransactionMessage;
                _logger.WriteEventViewerLog("LoadCurrentStatuses", e.Message, true);
            }
            finally
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.exitMessageInfo, "LoadCurrentStatuses"));
            }
        }

        /// <summary>
        /// Metodo que ejecuta la llamada al controlador para actualizar una estatus
        /// </summary>
        internal void UpdateStatus()
        {
            StatusEntity status = null;
            bool isAlreadyRegistered = false;
            try
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.enterMessageInfo, "UpdateStatus"));

                if (_view.SessionCurrentStatuses == null)
                    _view.SessionCurrentStatuses = _controller.SelectAllStatuses();

                if (!_controller.IsTheSameStatus(_view.StatusNameText, _view.SessionCurrentStatuses, _view.SessionCurrentStatus))
                    isAlreadyRegistered = _controller.StatusAlreadyRegistered(_view.StatusNameText, _view.SessionCurrentStatuses);

                if (!isAlreadyRegistered)
                {
                    status = _controller.CreateNewStatus(_view.StatusNameText);
                    status.Id = int.Parse(_view.SessionCurrentStatus.ToString());
                    if (_controller.UpdateStatus(status))
                    {
                        _view.SuccessfulTransaction = true;
                        _view.TransactionMessage = Messages.successfulTransactionMessage;
                    }
                }
                else
                {
                    _view.SuccessfulTransaction = false;
                    _view.TransactionMessage = Messages.duplicatedStatusMessage;
                }

                HttpContext.Current.Trace.Write(Messages.successfulTransactionMessage);
            }
            catch (ImperialException ie)
            {
                _view.SuccessfulTransaction = false;
                _view.TransactionMessage = ie.ImperialMessage;
            }
            catch (Exception e)
            {
                _view.SuccessfulTransaction = false;
                _view.TransactionMessage = Messages.failureTransactionMessage;
                _logger.WriteEventViewerLog("UpdateStatus", e.Message, true);
            }
            finally
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.exitMessageInfo, "UpdateStatus"));
            }
        }
    }
}